package lang;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class TestQueue {

	public static void main(String[] args) {
		BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
		Thread thProducer = new Thread(new Producer(queue));
		Thread thConsumer = new Thread(new Consumer(queue));
		thProducer.start();
		thConsumer.start();
		

	}

	static class Producer implements Runnable {
		private final BlockingQueue<Integer> queue;

		public Producer(BlockingQueue<Integer> queue) {
			this.queue = queue;
		}

		@Override
		public void run() {
			for (int i = 0; i < 10000; i++) {
				try {
					System.out.println("Producer"+i);
					queue.put(i);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	static class Consumer implements Runnable {

		private final BlockingQueue<Integer> queue;

		public Consumer(BlockingQueue<Integer> queue) {
			this.queue = queue;
		}

		@Override
		public void run() {
			for(;;) {
				try {
					System.out.println("consumer"+queue.take());
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

}
